var index = require('./index');
var user = require('./login/login');
var data = require('./common/data');
var excel = require('./common/excel');
var message = require('./message/message')
var group = require('./group/group')
var schedule = require('./schedule/schedule')
var git = require('./git/git')
var file = require('./common/file')

// 白明单
// 路径全匹配
var passUrl = [
    { url: '/' },
    { url: '/works' },
    { url: '/works/' },
    { url: '/works/data/time' },
    { url: '/works/login'},
    { url: '/works/loginByToken'},
    { url: '/works/logout'},
    { url: '/works/data/find/backlogs' },
    { url: '/works/data/find/sharedocs' },
    { url: '/works/data/find/attach' },
    { url: '/works/message/buildNotify' },
    { url: '/works/message/sendMessages' },
    { url: '/works/data/save/restApi' },
    { url: '/works/data/save/poModel' },
    { url: '/works/data/saveDocs/restApi' },
    { url: '/works/data/saveDocs/poModel' }
];

// 前缀匹配
var preUrl = [
    { url: '/template/' },
    { url: '/works/static' },
    { url: '/works/views' },
    { url: '/works/data/find/imgs' },
    { url: '/works/schedule/deployCheck' },
    { url: '/works/git/commit' },
    { url: '/works/file' }
];

module.exports = {
    initRoutes(server) {
        server.use('/', index);
        server.use('/works', user);
        server.use('/works/data', data);
        server.use('/works/excel', excel);
        server.use('/works/message', message);
        server.use('/works/group', group);
        server.use('/works/schedule', schedule);
        server.use('/work/schedule', schedule);
        server.use('/works/git', git);
        server.use('/work/git', git);
        server.use('/works/file', file);
        server.use('/work/file', file);
    },

    routeRule(req, res){      
        console.log("鉴权开始");
        console.log("请求URL：" + req.url);  
        var result = false;

        // 已登录
        if(req.cookies['user']){
            res.cookie("user", req.cookies['user'], { maxAge: 1000 * 60 * 30, sameSite: false });
            result = true;
        }
        
        if(passUrl.find((item) => item.url === req.url)){
            result = true;
        }        
        
        if(preUrl.find((item) => req.url.indexOf(item.url) != -1)){
            result = true;
        }
        return result;
    }
}